package cn.usersmsg.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.usersmsg.biz.IUserBiz;
import cn.usersmsg.biz.UserBizIpml;

public class DeleteUsers extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		String item = req.getParameter("item");
		IUserBiz iub = new UserBizIpml();
		if ("one".equals(item)) {
			int userid = Integer.parseInt(req.getParameter("userid"));
			
			boolean b = iub.delUser(userid);
			if (b) {
				out.print("<script>alert('删除成功');location.href='ShowUsers';</script>");
			}else{
				out.print("<script>alert('删除失败');location.href='ShowUsers';</script>");
			}
		}else{
			boolean b = false;
			int n = 0;
			String [] users = req.getParameterValues("checks");
			if (users!=null) {			
			
				for (int i = 0; i < users.length; i++) {
					int userid= Integer.parseInt(users[i]);
					b = iub.delUser(userid);
					if (b) {
						n++;
					}
				}
				out.print("<script>alert('"+n+"条数据删除成功');location.href='ShowUsers';</script>");
		
			}else{
				out.print("<script>alert('没有选中数据');location.href='ShowUsers';</script>");
			}
		}
	}

}
